package own.stu.jobgib.playown.juc.threadLocal;

//import com.alibaba.ttl.TransmittableThreadLocal;
//import com.alibaba.ttl.TtlRunnable;

//import com.alibaba.crr.composite.Backup;
//import com.alibaba.crr.composite.Capture;
//import com.alibaba.ttl3.TransmittableThreadLocal;
import com.alibaba.ttl.TransmittableThreadLocal;
//import com.alibaba.ttl3.TtlRunnable;
//import com.alibaba.ttl3.transmitter.Transmitter;
import com.alibaba.ttl.TtlRunnable;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TTLTest_2 {
    public static void main(String[] args) throws Exception {

//        testRunnable();
//        testRunnable2();
    }


    /*l
    注意hashCode的问题，lombok 没有根据地址 计算hashcode
     */
    @lombok.Data
    @AllArgsConstructor
    static class Data {
        String name;
        Integer id;
    }

    private static void testRunnable() throws Exception {
        TransmittableThreadLocal<Data> threadLocal = new TransmittableThreadLocal<>();
        threadLocal.set(new Data("lio", 1));

        TransmittableThreadLocal<String> tl2 = new TransmittableThreadLocal<>();
        tl2.set("parent value ");

        TtlRunnable ttlRunnable = TtlRunnable.get(() -> {
            log.info("child thread - {} get_v1 {}, get_v2 {}",
                    Thread.currentThread().getName(), threadLocal.get(), tl2.get());
        });

        ExecutorService service = Executors.newFixedThreadPool(2);
        service.submit(ttlRunnable).get();

        threadLocal.set(new Data("diva", 44));

        ttlRunnable = TtlRunnable.get(() -> {
            log.info("child thread - {} get_v1 {}, get_v2 {}",
                    Thread.currentThread().getName(), threadLocal.get(), tl2.get());
            threadLocal.get().setId(2);
        });
        service.submit(ttlRunnable).get();

        log.info("After -> t: {} get_v1 {}, get_v2 {}",
                Thread.currentThread().getName(), threadLocal.get(), tl2.get());

        service.shutdown();
    }

    /*private static void testRunnable2() throws Exception {
        Thread t = new Thread(() -> {
            TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
            threadLocal.set("value-set-in-parent");

            Capture capture = Transmitter.capture();

            new Thread(() -> {
                Backup backup = Transmitter.replay(capture);

                System.out.println(Thread.currentThread().getName() + " : " + threadLocal.get());
                threadLocal.set("value - set - by -child");
                System.out.println(Thread.currentThread().getName() + " : " + threadLocal.get());

                Transmitter.restore(backup);

                System.out.println(Thread.currentThread().getName() + " : " + threadLocal.get());

            }, "t-child - 1").start();
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + " : " + threadLocal.get());
        });

        t.start();
        t.join();


    }*/
}
